package reduce.fz.com.reduce.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.VideoCardDetailActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.VideoCard;
import reduce.fz.com.reduce.bean.VideoCardReply;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.custom.delegate.RecyclerViewDelegate;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ScreenUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyVideoCardListFragment extends BaseListFragment<MyVideoCardListFragment.MyVideoViewHolder, VideoCard> {
    private Long uid;

    public static MyVideoCardListFragment create(int position,Long uid) {
        MyVideoCardListFragment fragment = new MyVideoCardListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(BUNDLE_FRAGMENT_INDEX, position);
        bundle.putLong("uid",uid);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uid=getArguments().getLong("uid");
    }

    public interface VideoCardListener {
        public void VideoThumClick(VideoCard card);
    }


    public MyVideoCardListFragment() {
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
        return layoutManager;
    }


    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<VideoCard>>(new TypeReference<Result<List<VideoCard>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();

            }

            @Override
            public void responseInfo(Result<List<VideoCard>> data) {
                addResult(data);
            }
        });
    }

    @Override
    protected MyVideoViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_my_video_card, parent, false);
        MyVideoViewHolder holder = new MyVideoViewHolder(view, new VideoCardListener() {
            @Override
            public void VideoThumClick(VideoCard card) {
                forwardDetail(card);

            }
        });
        return holder;
    }

    public void forwardDetail(VideoCard card) {
        Intent intent = new Intent(this.getActivity(), VideoCardDetailActivity.class);
        intent.putExtra("cardid", card.cardid);
        startActivity(intent);
    }

    @Override
    protected MyVideoViewHolder createfootViewHolder(View view) {
        return new MyVideoViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<VideoCard> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_my_video_card,uid, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return 0;
    }

    private RecyclerViewDelegate mRecyclerViewDelegate = new RecyclerViewDelegate();

    public boolean isViewBeingDragged(MotionEvent event) {
        return mRecyclerViewDelegate.isViewBeingDragged(event, super.recyclerView);
    }

    public static class MyVideoViewHolder extends BaseRecycleViewHolder<VideoCard> {
        private NetworkImageView mVideoThum;
        private VideoCardListener listener;

        public int getWidth() {
            int width = ScreenUtils.getScreenW() / 2;
            return width;
        }

        public MyVideoViewHolder(View itemView, VideoCardListener listener) {
            this(itemView);
            mVideoThum = (NetworkImageView) itemView.findViewById(R.id.niv_video_thum);
            this.listener = listener;
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(getWidth(), getWidth());
            mVideoThum.setLayoutParams(lp);
        }

        public MyVideoViewHolder(View itemView) {
            super(itemView);

        }

        @Override
        public void renderView(final VideoCard data, int position) {
            mVideoThum.setImageUrl(data.video.thumUrl, HttpUtils.getImageLoader());
            mVideoThum.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.VideoThumClick(data);
                }
            });

        }
    }


}
